<?php
namespace App\Common\Server\Api\Lib;

use App\Common as com;
use App as nameApp;
use App\Common\Server\Api\Gateway as nameGateway;

class ApplicationParamDto extends nameApp\Api\Base implements \ArrayAccess {

	use com\Lib\Com\GoDataPool\Traits;

	/**
	 * @var array
	 */
	private array $array;

	public function __construct( private array $apiParam = [] ) {
		parent::__construct();
	}

	/**
	 * @return array
	 */
	public function getApiParam():array {
		return $this->apiParam;
	}

	/**
	 * @param array $apiParam
	 * @return static
	 */
	public function setApiParam( array $apiParam ):static {
		$this->apiParam = $apiParam;
		// 销毁部分对象
		$this->goDataPool()->unset( 'goField' );
		//
		return $this;
	}

	/**
	 * @param $offset
	 * @return bool
	 */
	function offsetExists( $offset ):bool {
		return isset( $this->array );
	}

	/**
	 * @param $offset
	 * @return mixed
	 */
	function offsetGet( $offset ):mixed {
		return $this->array[ $offset ] ?? null;
	}

	/**
	 * @param $offset
	 * @param $value
	 * @return void
	 */
	function offsetSet( $offset, $value ):void {
		if( is_null( $offset ) ){
			$this->array[] = $value;
		} else{
			$this->array[ $offset ] = $value;
		}
	}

	/**
	 * @param $offset
	 * @return void
	 */
	function offsetUnset( $offset ):void {
		unset( $this->array[ $offset ] );
	}

	/**
	 * 获取field字段判断对象
	 * @return com\Server\Api\Lib\ParamField
	 * @throws
	 */
	public function goField():com\Server\Api\Lib\ParamField {
		return $this->goDataPool()->get( __FUNCTION__, function () {
			return com\Server\Api\Lib\ParamField::new( $apiParam['field'] ?? [] );
		} );
	}

	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * @param array $apiParam
	 * @return static
	 */
	public static function makeOfNew( array $apiParam ):static {
	    return self::make( $apiParam );
	}

	/**
	 * @param nameGateway\ApiRequest $apiRequest
	 * @return static
	 */
	public static function makeByApiRequest( nameGateway\ApiRequest $apiRequest ):static {
	    return self::makeOfNew( $apiRequest->apiData()->apiParam()->get() );
	}

}
